home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Demos / TimeCPU.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  21KB  |  924 lines

  1. /*
  2. TimeCPU.c
  3. Denis G. Pelli, 1991-1994
  4. This routine uses Timer.c to measure the timing of basic CPU
  5. operations and several random number generators. The Fixed data type is
  6. predefined by Apple as a long (i.e. 32 bits) with an assumed decimal point in
  7. the middle.
  8.  
  9. The access to video memory overwrites a small part of your main screen, the
  10. first 40 bytes. This will be in the upper left hand corner of your display, and
  11. will usually be barely noticeable. I like seeing that, as it confirms that the
  12. program really is accessing the video memory.
  13.  
  14. This program requests a memory partition of 900K. It will run in less, but will
  15. then do fewer iterations.
  16.  
  17. The timing seems to be very accurate. It ought to be as accurate as the
  18. frequency of the oscillator in the VIA chip. However, I haven't checked the
  19. timing against a known standard.
  20.  
  21. HISTORY:
  22. 1/91    dgp    wrote it
  23. 2/16/91    dgp    added fpu test, to fail gracefully if compiled with FPU support, but FPU
  24.             is not present.
  25. 3/4/91    dgp    added timing of random number generators
  26. 8/6/91    dgp    added timing of RandFill.
  27. 8/24/91    dgp    Made compatible with THINK C 5.0.
  28. 1/25/92    dgp    Calibrate and correct for the slowness of TimeIt(). 
  29.             Measure and subtract off the small loop overhead.
  30.             Identify machine and compiler.
  31.             Automatically append results to TimeCPU.data file.
  32. 1/29/92    dgp    Time move from memory to video memory, for showing movies.
  33.             Added transcendental functions since Radius 8881 init and System 7.01
  34.             speed them up dramatically and the Quadra is reputed to
  35.             do them very slowly.
  36. 1/30/92    dgp    Access video memory only in 32-bit mode, to avoid crashes.
  37. 3/10/92    dgp    include mc68881.h
  38. 8/19/92    dgp    time the 68881 instructions _sin, _sqrt, _exp, _log
  39.             Use new Timer.c instead of old TimeIt.c
  40. 8/28/92    dgp    updated to use new reentrant Timer.c
  41. 11/18/92 dgp renamed output file to “TimeCPU results”
  42. 1/11/93    dgp    check for presence of 68020. Put Gestalt tests in main, without
  43.             any fpu usage, since program was crashing when fpu was absent
  44.             before getting to the fpu test. (Supposedly that was fixed
  45.             in THINK C 5. Oh well.)
  46. 2/7/93    dgp    added timing of SetPixelsQuickly().
  47. 7/9/93    dgp check for 32-bit addressing capability.
  48. 3/13/94    dgp    added timing of short arithmetic, and put conditionals around each section.
  49. 6/1/94    dgp    added timing of BlockMoveData().
  50. 6/14/94    dgp    can32 is now computed by calling TrapAvailable(_SwapMMUMode), which 
  51.             returns the correct answer even on Macs with dirty ROMs.
  52. 7/31/94    dgp made compatible with new SANE.h in Universal Headers.
  53. 9/5/94 dgp removed assumption in printf's that int==short.
  54. 10/2/94    dgp deleted RandomX() since it's part of SANE, which doesn't exist on PowerPC,
  55.             and, in any event, was uselessly slow on 68k machines. Tidied up the
  56.             printout for readability even for computers as fast as the PowerPC.
  57. 4/9/95 dgp added a few tests relevant to nrand(). Increased n. Polished the printout a bit.
  58. 4/11/95 dgp changed declaration of bufferHandle from void ** to Handle, for compatibility with
  59. old pre-universal apple headers.
  60. */
  61. #include "VideoToolbox.h"
  62. #include <assert.h>
  63. #include <math.h>
  64. #include <Traps.h>    // _SwapMMUMode
  65. #include <Menus.h>    // DrawMenuBar
  66. #include "mc68881.h"
  67.  
  68. void TimeCPU(void);
  69.  
  70. void main(void)
  71. {
  72.     long value;
  73.  
  74.     Require(gestaltOriginalQD);
  75.     Gestalt(gestaltTimeMgrVersion,&value);
  76.     if(value<gestaltRevisedTimeMgr)
  77.         PrintfExit("Sorry, your System is too old; I need at least \n"
  78.             "the Revised Time Manager.\n");
  79.     TimeCPU();
  80. }
  81.  
  82. void TimeCPU(void)
  83. {
  84.     long n,quickDraw;
  85.     short i;
  86.     register long *paL,*pbL,iL,jL,kL,mL;
  87.     double x,y,z;
  88.     Fixed xF,yF,zF;
  89.     Handle bufferHandle;
  90.     void *buffer,*buffer2;
  91.     char Buffer[32];
  92.     FILE *o[2],*dataFile;
  93.     GDHandle device;
  94.     long startTicks,finalTicks;
  95.     double s,s0,overhead;
  96.     Timer *timer;
  97.     Boolean can32;
  98.     OSErr osErr;
  99.  
  100.     MaximizeConsoleHeight();
  101.     printf("Welcome to TimeCPU.\n");
  102.     assert(StackSpace()>4000);
  103.     can32=TrapAvailable(_SwapMMUMode);
  104.     timer=NewTimer();
  105.     Delay(1,&startTicks);
  106.     StartTimer(timer);
  107.     Delay(60,&finalTicks);
  108.     s=StopTimerSecs(timer);
  109.     printf("The VBL tick frequency is %.2f Hz.\n",(finalTicks-startTicks)/s);
  110.     o[0]=stdout;
  111.     o[1]=dataFile=fopen("TimeCPU results","a");    /* Append to data file */
  112.     if(dataFile!=NULL){
  113.         printf("Key results will be appended to “TimeCPU results” file.\n\n");
  114.         SetFileInfo("TimeCPU results",'TEXT','ttxt');
  115.     }
  116.     else printf("Could not open “TimeCPU results” file\n\n");
  117.     ffprintf(o,"\n%s\n",BreakLines(IdentifyMachine(),80));
  118.     ffprintf(o,"%s\n\n",BreakLines(IdentifyCompiler(),80));
  119.     ffprintf(o,"      Time    Operation\n");
  120.     srand(clock());
  121.     y=sqrt(2.0);
  122.     z=sqrt(3.0);
  123.     kL=y*1000.;
  124.     mL=z*10.;
  125.     RandFill(Buffer,sizeof(Buffer));
  126.         
  127.     n=100000;    // REDUCE THIS NUMBER TO SPEED UP THE TESTING.
  128.     overhead=0.0;
  129.     StartTimer(timer);
  130.     for(iL=n/10;iL>0;iL--) ;
  131.     overhead=StopTimerSecs(timer)/n-overhead;    // the loop overhead per operation
  132.  
  133.     Gestalt(gestaltQuickdrawVersion,&quickDraw);
  134.     if(quickDraw>=gestalt8BitQD){
  135.         unsigned long row[256],row2[256];
  136.         int rowLength=256,clutSize;
  137.         
  138.         assert(StackSpace()>4000);
  139.         n/=100;
  140.         device=GetMainDevice();
  141.         clutSize=GDClutSize(device);
  142.         for(i=0;i<rowLength;i++)row[i]=nrand(clutSize);
  143.         StartTimer(timer);
  144.         for(iL=n;iL>0;iL--){
  145.             SetDevicePixelsQuickly(device,0,0,row,rowLength);
  146.         }
  147.         // NOTE: if you single step through this in the Debugger
  148.         // you'll get an apparent read-back error if the Debugger
  149.         // redraws the Menu bar between writing and reading the pixels.
  150.         s=StopTimerSecs(timer)/n-overhead;
  151.         GetDevicePixelsQuickly(device,0,0,row2,rowLength);
  152.         ffprintf(o,"%11.3f ms    SetPixelsQuickly(,,,,%d);// %d-bit pixels, %.3f MB/s\n",s*1e3
  153.             ,(int)rowLength,(int)(**(**device).gdPMap).pixelSize
  154.             ,(double)rowLength*(**(**device).gdPMap).pixelSize/8/1024/1024/s);
  155.  
  156.         StartTimer(timer);
  157.         for(iL=n;iL>0;iL--){
  158.             GetDevicePixelsQuickly(device,0,0,row,rowLength);
  159.         }
  160.         s=StopTimerSecs(timer)/n-overhead;
  161.         ffprintf(o,"%11.3f ms    GetPixelsQuickly(,,,,%d);// %d-bit pixels, %.3f MB/s\n",s*1e3
  162.             ,(int)rowLength,(int)(**(**device).gdPMap).pixelSize
  163.             ,(double)rowLength*(**(**device).gdPMap).pixelSize/8/1024/1024/s);
  164.         n*=100;
  165.  
  166.         for(i=0;i<1;i++)if(row2[i]!=row[i])printf("Pixel %d: wrote %ld != read %ld\n"
  167.             ,(int)i,row[i],row2[i]);
  168.     }
  169.     
  170.     for(;n>0;){
  171.         bufferHandle=NewHandle(2*sizeof(long)*n);
  172.         if(bufferHandle==NULL)bufferHandle=TempNewHandle(2*sizeof(long)*n,&osErr);
  173.         if(bufferHandle!=NULL)break;
  174.         n/=2;
  175.         printf("Reducing iterations to %ld to fit in available memory.\n",n);
  176.     }
  177.     assert(bufferHandle!=NULL);
  178.     HLockHi(bufferHandle);
  179.     buffer=*bufferHandle;
  180.     buffer2=(long *)buffer+n;
  181.     StartTimer(timer);
  182.     BlockMove(buffer2,buffer,4*n);
  183.     s=StopTimerSecs(timer);
  184.     ffprintf(o,"%11.3f ms    BlockMove(,,%ld);    // %.3f µs/byte or %.1f MB/s\n"
  185.         ,s*1e3,n*4,s*1e6/(n*4),n*4/1024./1024./s);
  186.     StartTimer(timer);
  187.     BlockMoveData(buffer2,buffer,4*n);
  188.     s=StopTimerSecs(timer);
  189.     ffprintf(o,"%11.3f ms    BlockMoveData(,,%ld);    // %.3f µs/byte or %.1f MB/s\n"
  190.         ,s*1e3,n*4,s*1e6/(n*4),n*4/1024./1024./s);
  191.     paL=(long *)buffer;
  192.     pbL=(long *)buffer2;
  193.     StartTimer(timer);
  194.     for(iL=n/10;iL>0;iL--){
  195.         *paL++=*pbL++;
  196.         *paL++=*pbL++;
  197.         *paL++=*pbL++;
  198.         *paL++=*pbL++;
  199.         *paL++=*pbL++;
  200.         *paL++=*pbL++;
  201.         *paL++=*pbL++;
  202.         *paL++=*pbL++;
  203.         *paL++=*pbL++;
  204.         *paL++=*pbL++;
  205.     }
  206.     s=StopTimerSecs(timer)/n-overhead;
  207.     ffprintf(o,"%11.3f µs    *paL++=*pbL++;        // long, memory to memory, %.1f MB/s\n",s*1e6,4e-6/s);
  208.  
  209.     Gestalt(gestaltQuickdrawVersion,&quickDraw);
  210.     if(quickDraw>=gestalt8BitQD){
  211.         device=GetMainDevice();
  212.         if(device!=NULL){
  213.             paL=(long *)(**(**device).gdPMap).baseAddr;
  214.             if(paL!=NULL){
  215.                 char mode=true32b;
  216.                 long *pSave=paL;
  217.                 
  218.                 StartTimer(timer);
  219.                 if(can32)SwapMMUMode(&mode);
  220.                 for(iL=n/10;iL>0;iL--) paL-=10;
  221.                 if(can32)SwapMMUMode(&mode);
  222.                 s0=StopTimerSecs(timer)/n-overhead;
  223.                 paL=pSave;
  224.                 pbL=(long *)buffer2;
  225.                 StartTimer(timer);
  226.                 if(can32)SwapMMUMode(&mode);
  227.                 for(iL=n/10;iL>0;iL--){
  228.                     *paL++=*pbL++;
  229.                     *paL++=*pbL++;
  230.                     *paL++=*pbL++;
  231.                     *paL++=*pbL++;
  232.                     *paL++=*pbL++;
  233.                     *paL++=*pbL++;
  234.                     *paL++=*pbL++;
  235.                     *paL++=*pbL++;
  236.                     *paL++=*pbL++;
  237.                     *paL++=*pbL++;
  238.                     paL-=10;
  239.                 }
  240.                 if(can32)SwapMMUMode(&mode);
  241.                 s=StopTimerSecs(timer)/n-overhead;
  242.                 s-=s0;                                    // remove time for the paL-=10;
  243.                 ffprintf(o,"%11.3f µs    *paL++=*pbL++;        // long, memory to video mem. %.1f MB/s\n",s*1e6,4e-6/s);
  244.  
  245.                 if(0){
  246.                     // doesn't always work, alas
  247.                     paL=pSave;
  248.                     pbL=(long *)buffer2;
  249.                     StartTimer(timer);
  250.                     BlockMoveData(pbL,paL,4*n);
  251.                     s=StopTimerSecs(timer)-overhead;
  252.                     ffprintf(o,"%11.3f ms    BlockMoveData(,,%ld);    // i.e. %.3f µs/byte\n"
  253.                         ,s*1e3,n*4,s*1e6/(n*4));
  254.                 }
  255.             }
  256.         }
  257.     }
  258.     DisposeHandle(bufferHandle);
  259.     buffer=buffer2=NULL;
  260.  
  261.     if(1){            // time long arithmetic
  262.         StartTimer(timer);
  263.         for(iL=n/10;iL>0;iL--){
  264.             jL=kL;
  265.             jL=kL;
  266.             jL=kL;
  267.             jL=kL;
  268.             jL=kL;
  269.             jL=kL;
  270.             jL=kL;
  271.             jL=kL;
  272.             jL=kL;
  273.             jL=kL;
  274.         }
  275.         s=StopTimerSecs(timer)/n-overhead;
  276.         ffprintf(o,"%11.3f µs    jL=kL;            // long, register to register\n",s*1e6);
  277.     
  278.         StartTimer(timer);
  279.         for(iL=n/10;iL>0;iL--){
  280.             jL=kL>>1;
  281.             jL=kL>>1;
  282.             jL=kL>>1;
  283.             jL=kL>>1;
  284.             jL=kL>>1;
  285.             jL=kL>>1;
  286.             jL=kL>>1;
  287.             jL=kL>>1;
  288.             jL=kL>>1;
  289.             jL=kL>>1;
  290.         }
  291.         s=StopTimerSecs(timer)/n-overhead;
  292.         ffprintf(o,"%11.3f µs    jL=kL>>1;\n",s*1e6);
  293.     
  294.         StartTimer(timer);
  295.         for(iL=n/10;iL>0;iL--){
  296.             jL=kL+mL;
  297.             jL=kL+mL;
  298.             jL=kL+mL;
  299.             jL=kL+mL;
  300.             jL=kL+mL;
  301.             jL=kL+mL;
  302.             jL=kL+mL;
  303.             jL=kL+mL;
  304.             jL=kL+mL;
  305.             jL=kL+mL;
  306.         }
  307.         s=StopTimerSecs(timer)/n-overhead;
  308.         ffprintf(o,"%11.3f µs    jL=kL+mL;\n",s*1e6);
  309.     
  310.         StartTimer(timer);
  311.         for(iL=n/10;iL>0;iL--){
  312.             jL=kL-mL;
  313.             jL=kL-mL;
  314.             jL=kL-mL;
  315.             jL=kL-mL;
  316.             jL=kL-mL;
  317.             jL=kL-mL;
  318.             jL=kL-mL;
  319.             jL=kL-mL;
  320.             jL=kL-mL;
  321.             jL=kL-mL;
  322.         }
  323.         s=StopTimerSecs(timer)/n-overhead;
  324.         ffprintf(o,"%11.3f µs    jL=kL-mL;\n",s*1e6);
  325.     
  326.         n/=10;            /* all other operations take at least several microseconds */
  327.         StartTimer(timer);
  328.         for(iL=n/10;iL>0;iL--){
  329.             jL=kL*mL;
  330.             jL=kL*mL;
  331.             jL=kL*mL;
  332.             jL=kL*mL;
  333.             jL=kL*mL;
  334.             jL=kL*mL;
  335.             jL=kL*mL;
  336.             jL=kL*mL;
  337.             jL=kL*mL;
  338.             jL=kL*mL;
  339.         }
  340.         s=StopTimerSecs(timer)/n-overhead;
  341.         ffprintf(o,"%11.3f µs    jL=kL*mL;\n",s*1e6);
  342.     
  343.         StartTimer(timer);
  344.         for(iL=n/10;iL>0;iL--){
  345.             jL=kL/mL;
  346.             jL=kL/mL;
  347.             jL=kL/mL;
  348.             jL=kL/mL;
  349.             jL=kL/mL;
  350.             jL=kL/mL;
  351.             jL=kL/mL;
  352.             jL=kL/mL;
  353.             jL=kL/mL;
  354.             jL=kL/mL;
  355.         }
  356.         s=StopTimerSecs(timer)/n-overhead;
  357.         ffprintf(o,"%11.3f µs    jL=kL/mL;\n",s*1e6);
  358.         n*=10;
  359.     }
  360.     if(1){                // time short arithmetic
  361.         register short jH,kH=1234,mH=5678;
  362.         
  363.         StartTimer(timer);
  364.         for(iL=n/10;iL>0;iL--){
  365.             jH=kH;
  366.             jH=kH;
  367.             jH=kH;
  368.             jH=kH;
  369.             jH=kH;
  370.             jH=kH;
  371.             jH=kH;
  372.             jH=kH;
  373.             jH=kH;
  374.             jH=kH;
  375.         }
  376.         s=StopTimerSecs(timer)/n-overhead;
  377.         ffprintf(o,"%11.3f µs    jH=kH;            // short, register to register\n",s*1e6);
  378.     
  379.         StartTimer(timer);
  380.         for(iL=n/10;iL>0;iL--){
  381.             jH=kH>>1;
  382.             jH=kH>>1;
  383.             jH=kH>>1;
  384.             jH=kH>>1;
  385.             jH=kH>>1;
  386.             jH=kH>>1;
  387.             jH=kH>>1;
  388.             jH=kH>>1;
  389.             jH=kH>>1;
  390.             jH=kH>>1;
  391.         }
  392.         s=StopTimerSecs(timer)/n-overhead;
  393.         ffprintf(o,"%11.3f µs    jH=kH>>1;\n",s*1e6);
  394.     
  395.         StartTimer(timer);
  396.         for(iL=n/10;iL>0;iL--){
  397.             jH=kH+mH;
  398.             jH=kH+mH;
  399.             jH=kH+mH;
  400.             jH=kH+mH;
  401.             jH=kH+mH;
  402.             jH=kH+mH;
  403.             jH=kH+mH;
  404.             jH=kH+mH;
  405.             jH=kH+mH;
  406.             jH=kH+mH;
  407.         }
  408.         s=StopTimerSecs(timer)/n-overhead;
  409.         ffprintf(o,"%11.3f µs    jH=kH+mH;\n",s*1e6);
  410.     
  411.         StartTimer(timer);
  412.         for(iL=n/10;iL>0;iL--){
  413.             jH=kH-mH;
  414.             jH=kH-mH;
  415.             jH=kH-mH;
  416.             jH=kH-mH;
  417.             jH=kH-mH;
  418.             jH=kH-mH;
  419.             jH=kH-mH;
  420.             jH=kH-mH;
  421.             jH=kH-mH;
  422.             jH=kH-mH;
  423.         }
  424.         s=StopTimerSecs(timer)/n-overhead;
  425.         ffprintf(o,"%11.3f µs    jH=kH-mH;\n",s*1e6);
  426.     
  427.         n/=10;            /* all other operations take at least several microseconds */
  428.         StartTimer(timer);
  429.         for(iL=n/10;iL>0;iL--){
  430.             jH=kH*mH;
  431.             jH=kH*mH;
  432.             jH=kH*mH;
  433.             jH=kH*mH;
  434.             jH=kH*mH;
  435.             jH=kH*mH;
  436.             jH=kH*mH;
  437.             jH=kH*mH;
  438.             jH=kH*mH;
  439.             jH=kH*mH;
  440.         }
  441.         s=StopTimerSecs(timer)/n-overhead;
  442.         ffprintf(o,"%11.3f µs    jH=kH*mH;\n",s*1e6);
  443.     
  444.         StartTimer(timer);
  445.         for(iL=n/10;iL>0;iL--){
  446.             jH=kH/mH;
  447.             jH=kH/mH;
  448.             jH=kH/mH;
  449.             jH=kH/mH;
  450.             jH=kH/mH;
  451.             jH=kH/mH;
  452.             jH=kH/mH;
  453.             jH=kH/mH;
  454.             jH=kH/mH;
  455.             jH=kH/mH;
  456.         }
  457.         s=StopTimerSecs(timer)/n-overhead;
  458.         ffprintf(o,"%11.3f µs    jH=kH/mH;\n",s*1e6);
  459.         n*=10;
  460.     }
  461.  
  462.     n/=10;            /* all other operations take at least several microseconds */
  463.  
  464.     if(1){        // time double arithmetic
  465.         StartTimer(timer);
  466.         for(iL=n/10;iL>0;iL--){
  467.             x=y;
  468.             x=y;
  469.             x=y;
  470.             x=y;
  471.             x=y;
  472.             x=y;
  473.             x=y;
  474.             x=y;
  475.             x=y;
  476.             x=y;
  477.         }
  478.         s=StopTimerSecs(timer)/n-overhead;
  479.         ffprintf(o,"%11.3f µs    x=y;            // double\n",s*1e6);
  480.     
  481.         StartTimer(timer);
  482.         for(iL=n/10;iL>0;iL--){
  483.             x=y+z;
  484.             x=y+z;
  485.             x=y+z;
  486.             x=y+z;
  487.             x=y+z;
  488.             x=y+z;
  489.             x=y+z;
  490.             x=y+z;
  491.             x=y+z;
  492.             x=y+z;
  493.         }
  494.         s=StopTimerSecs(timer)/n-overhead;
  495.         ffprintf(o,"%11.3f µs    x=y+z;\n",s*1e6);
  496.     
  497.         StartTimer(timer);
  498.         for(iL=n/10;iL>0;iL--){
  499.             x=y-z;
  500.             x=y-z;
  501.             x=y-z;
  502.             x=y-z;
  503.             x=y-z;
  504.             x=y-z;
  505.             x=y-z;
  506.             x=y-z;
  507.             x=y-z;
  508.             x=y-z;
  509.         }
  510.         s=StopTimerSecs(timer)/n-overhead;
  511.         ffprintf(o,"%11.3f µs    x=y-z;\n",s*1e6);
  512.     
  513.         StartTimer(timer);
  514.         for(iL=n/10;iL>0;iL--){
  515.             x=y*z;
  516.             x=y*z;
  517.             x=y*z;
  518.             x=y*z;
  519.             x=y*z;
  520.             x=y*z;
  521.             x=y*z;
  522.             x=y*z;
  523.             x=y*z;
  524.             x=y*z;
  525.         }
  526.         s=StopTimerSecs(timer)/n-overhead;
  527.         ffprintf(o,"%11.3f µs    x=y*z;\n",s*1e6);
  528.     
  529.         StartTimer(timer);
  530.         for(iL=n/10;iL>0;iL--){
  531.             x=y/z;
  532.             x=y/z;
  533.             x=y/z;
  534.             x=y/z;
  535.             x=y/z;
  536.             x=y/z;
  537.             x=y/z;
  538.             x=y/z;
  539.             x=y/z;
  540.             x=y/z;
  541.         }
  542.         s=StopTimerSecs(timer)/n-overhead;
  543.         ffprintf(o,"%11.3f µs    x=y/z;\n",s*1e6);
  544.     }
  545.     
  546.     if(1){                // time transcendental functions
  547.         StartTimer(timer);
  548.         for(iL=n/10;iL>0;iL--){
  549.             x=sin(y);
  550.             x=sin(y);
  551.             x=sin(y);
  552.             x=sin(y);
  553.             x=sin(y);
  554.             x=sin(y);
  555.             x=sin(y);
  556.             x=sin(y);
  557.             x=sin(y);
  558.             x=sin(y);
  559.         }
  560.         s=StopTimerSecs(timer)/n-overhead;
  561.         ffprintf(o,"%11.3f µs    x=sin(y);\n",s*1e6);
  562.     
  563.         StartTimer(timer);
  564.         for(iL=n/10;iL>0;iL--){
  565.             x=sqrt(y);
  566.             x=sqrt(y);
  567.             x=sqrt(y);
  568.             x=sqrt(y);
  569.             x=sqrt(y);
  570.             x=sqrt(y);
  571.             x=sqrt(y);
  572.             x=sqrt(y);
  573.             x=sqrt(y);
  574.             x=sqrt(y);
  575.         }
  576.         s=StopTimerSecs(timer)/n-overhead;
  577.         ffprintf(o,"%11.3f µs    x=sqrt(y);\n",s*1e6);
  578.     
  579.         n/=100;
  580.         StartTimer(timer);
  581.         for(iL=n/10;iL>0;iL--){
  582.             x=log(y);
  583.             x=log(y);
  584.             x=log(y);
  585.             x=log(y);
  586.             x=log(y);
  587.             x=log(y);
  588.             x=log(y);
  589.             x=log(y);
  590.             x=log(y);
  591.             x=log(y);
  592.         }
  593.         s=StopTimerSecs(timer)/n-overhead;
  594.         ffprintf(o,"%11.3f µs    x=log(y);\n",s*1e6);
  595.         StartTimer(timer);
  596.         for(iL=n/10;iL>0;iL--){
  597.             x=exp(y);
  598.             x=exp(y);
  599.             x=exp(y);
  600.             x=exp(y);
  601.             x=exp(y);
  602.             x=exp(y);
  603.             x=exp(y);
  604.             x=exp(y);
  605.             x=exp(y);
  606.             x=exp(y);
  607.         }
  608.         s=StopTimerSecs(timer)/n-overhead;
  609.         ffprintf(o,"%11.3f µs    x=exp(y);\n",s*1e6);
  610.         n*=100;
  611.     
  612.         #if mc68881 && THINK_C
  613.             StartTimer(timer);
  614.             for(iL=n/10;iL>0;iL--){
  615.                 x=_sin(y);
  616.                 x=_sin(y);
  617.                 x=_sin(y);
  618.                 x=_sin(y);
  619.                 x=_sin(y);
  620.                 x=_sin(y);
  621.                 x=_sin(y);
  622.                 x=_sin(y);
  623.                 x=_sin(y);
  624.                 x=_sin(y);
  625.             }
  626.             s=StopTimerSecs(timer)/n-overhead;
  627.             ffprintf(o,"%11.3f µs    x=_sin(y);\n",s*1e6);
  628.         
  629.             StartTimer(timer);
  630.             for(iL=n/10;iL>0;iL--){
  631.                 x=_sqrt(y);
  632.                 x=_sqrt(y);
  633.                 x=_sqrt(y);
  634.                 x=_sqrt(y);
  635.                 x=_sqrt(y);
  636.                 x=_sqrt(y);
  637.                 x=_sqrt(y);
  638.                 x=_sqrt(y);
  639.                 x=_sqrt(y);
  640.                 x=_sqrt(y);
  641.             }
  642.             s=StopTimerSecs(timer)/n-overhead;
  643.             ffprintf(o,"%11.3f µs    x=_sqrt(y);\n",s*1e6);
  644.         
  645.             StartTimer(timer);
  646.             for(iL=n/10;iL>0;iL--){
  647.                 x=_log(y);
  648.                 x=_log(y);
  649.                 x=_log(y);
  650.                 x=_log(y);
  651.                 x=_log(y);
  652.                 x=_log(y);
  653.                 x=_log(y);
  654.                 x=_log(y);
  655.                 x=_log(y);
  656.                 x=_log(y);
  657.             }
  658.             s=StopTimerSecs(timer)/n-overhead;
  659.             ffprintf(o,"%11.3f µs    x=_log(y);\n",s*1e6);
  660.             
  661.             StartTimer(timer);
  662.             for(iL=n/10;iL>0;iL--){
  663.                 x=_exp(y);
  664.                 x=_exp(y);
  665.                 x=_exp(y);
  666.                 x=_exp(y);
  667.                 x=_exp(y);
  668.                 x=_exp(y);
  669.                 x=_exp(y);
  670.                 x=_exp(y);
  671.                 x=_exp(y);
  672.                 x=_exp(y);
  673.             }
  674.             s=StopTimerSecs(timer)/n-overhead;
  675.             ffprintf(o,"%11.3f µs    x=_exp(y);\n",s*1e6);
  676.         #endif
  677.     }
  678.  
  679.     o[1]=NULL;        /* that's all we want to save in “TimeCPU results” */
  680.  
  681.     if(1){                    // time Fixed
  682.         yF=zF=0x12341234;
  683.         StartTimer(timer);
  684.         for(iL=n/10;iL>0;iL--){
  685.             xF=FixMul(yF,zF);
  686.             xF=FixMul(yF,zF);
  687.             xF=FixMul(yF,zF);
  688.             xF=FixMul(yF,zF);
  689.             xF=FixMul(yF,zF);
  690.             xF=FixMul(yF,zF);
  691.             xF=FixMul(yF,zF);
  692.             xF=FixMul(yF,zF);
  693.             xF=FixMul(yF,zF);
  694.             xF=FixMul(yF,zF);
  695.         }
  696.         s=StopTimerSecs(timer)/n-overhead;
  697.         ffprintf(o,"%11.3f µs    xF=FixMul(yF,zF);    // Fixed\n",s*1e6);
  698.     
  699.         yF=(long)(PI*256);
  700.         zF=(long)(1.1*256);
  701.         StartTimer(timer);
  702.         for(iL=n/10;iL>0;iL--){
  703.             xF=FixDiv(yF,zF);
  704.             xF=FixDiv(yF,zF);
  705.             xF=FixDiv(yF,zF);
  706.             xF=FixDiv(yF,zF);
  707.             xF=FixDiv(yF,zF);
  708.             xF=FixDiv(yF,zF);
  709.             xF=FixDiv(yF,zF);
  710.             xF=FixDiv(yF,zF);
  711.             xF=FixDiv(yF,zF);
  712.             xF=FixDiv(yF,zF);
  713.         }
  714.         s=StopTimerSecs(timer)/n-overhead;
  715.         ffprintf(o,"%11.3f µs    xF=FixDiv(yF,zF);\n",s*1e6);
  716.     
  717.         StartTimer(timer);
  718.         for(iL=n/10;iL>0;iL--){
  719.             xF=FixRatio(123,1234);
  720.             xF=FixRatio(123,1234);
  721.             xF=FixRatio(123,1234);
  722.             xF=FixRatio(123,1234);
  723.             xF=FixRatio(123,1234);
  724.             xF=FixRatio(123,1234);
  725.             xF=FixRatio(123,1234);
  726.             xF=FixRatio(123,1234);
  727.             xF=FixRatio(123,1234);
  728.             xF=FixRatio(123,1234);
  729.         }
  730.         s=StopTimerSecs(timer)/n-overhead;
  731.         ffprintf(o,"%11.3f µs    xF=FixRatio(123,1234);\n",s*1e6);
  732.     
  733.         StartTimer(timer);
  734.         for(iL=n/10;iL>0;iL--){
  735.             xF=DoubleToFix(y);
  736.             xF=DoubleToFix(y);
  737.             xF=DoubleToFix(y);
  738.             xF=DoubleToFix(y);
  739.             xF=DoubleToFix(y);
  740.             xF=DoubleToFix(y);
  741.             xF=DoubleToFix(y);
  742.             xF=DoubleToFix(y);
  743.             xF=DoubleToFix(y);
  744.             xF=DoubleToFix(y);
  745.         }
  746.         s=StopTimerSecs(timer)/n-overhead;
  747.         ffprintf(o,"%11.3f µs    xF=DoubleToFix(y);\n",s*1e6);
  748.     
  749.         xF=(long)(PI*256);
  750.         StartTimer(timer);
  751.         for(iL=n/10;iL>0;iL--){
  752.             x=FixToDouble(xF);
  753.             x=FixToDouble(xF);
  754.             x=FixToDouble(xF);
  755.             x=FixToDouble(xF);
  756.             x=FixToDouble(xF);
  757.             x=FixToDouble(xF);
  758.             x=FixToDouble(xF);
  759.             x=FixToDouble(xF);
  760.             x=FixToDouble(xF);
  761.             x=FixToDouble(xF);
  762.             x=FixToDouble(xF);
  763.         }
  764.         s=StopTimerSecs(timer)/n-overhead;
  765.         ffprintf(o,"%11.3f µs    x=FixToDouble(xF);\n",s*1e6);
  766.     }
  767.     
  768.     if(1){                // time rand()
  769.         n*=10;
  770.         for(;n>0;){
  771.             bufferHandle=NewHandle(n);
  772.             if(bufferHandle==NULL)bufferHandle=TempNewHandle(n,&osErr);
  773.             if(bufferHandle!=NULL)break;
  774.             n/=2;
  775.             printf("Reducing iterations to %ld to fit in available memory.\n",n);
  776.         }
  777.         assert(bufferHandle!=NULL);
  778.         HLockHi(bufferHandle);
  779.         buffer=*bufferHandle;
  780.         StartTimer(timer);
  781.         s=StopTimerSecs(timer);
  782.         StartTimer(timer);
  783.         RandFill(buffer,n);
  784.         s=StopTimerSecs(timer) - s;
  785.         DisposeHandle(bufferHandle);
  786.         buffer=NULL;
  787.         ffprintf(o,"%11.3f µs    RandFill(,%ld);    // i.e. %4.1f µs/byte\n"
  788.             ,s*1e6,n,s*1e6/n);
  789.         n/=10;
  790.     
  791.         StartTimer(timer);
  792.         for(iL=n/10;iL>0;iL--){
  793.             i=randU();
  794.             i=randU();
  795.             i=randU();
  796.             i=randU();
  797.             i=randU();
  798.             i=randU();
  799.             i=randU();
  800.             i=randU();
  801.             i=randU();
  802.             i=randU();
  803.         }
  804.         s=StopTimerSecs(timer)/n-overhead;
  805.         ffprintf(o,"%11.3f µs    i=randU();        // i.e. %4.1f µs/byte\n",s*1e6, s*1e6/2.);
  806.     
  807.         StartTimer(timer);
  808.         for(iL=n/10;iL>0;iL--){
  809.             i=rand();
  810.             i=rand();
  811.             i=rand();
  812.             i=rand();
  813.             i=rand();
  814.             i=rand();
  815.             i=rand();
  816.             i=rand();
  817.             i=rand();
  818.             i=rand();
  819.         }
  820.         s=StopTimerSecs(timer)/n-overhead;
  821.         ffprintf(o,"%11.3f µs    i=rand();         // i.e. %4.1f µs/byte\n",s*1e6, s*1e6/1.);
  822.     
  823.         StartTimer(timer);
  824.         for(iL=n/10;iL>0;iL--){
  825.             i=Random();
  826.             i=Random();
  827.             i=Random();
  828.             i=Random();
  829.             i=Random();
  830.             i=Random();
  831.             i=Random();
  832.             i=Random();
  833.             i=Random();
  834.             i=Random();
  835.         }
  836.         s=StopTimerSecs(timer)/n-overhead;
  837.         ffprintf(o,"%11.3f µs    i=Random();\n",s*1e6);
  838.     
  839.         StartTimer(timer);
  840.         for(iL=n/10;iL>0;iL--){
  841.             i=nrand(127);
  842.             i=nrand(127);
  843.             i=nrand(127);
  844.             i=nrand(127);
  845.             i=nrand(127);
  846.             i=nrand(127);
  847.             i=nrand(127);
  848.             i=nrand(127);
  849.             i=nrand(127);
  850.             i=nrand(127);
  851.         }
  852.         s=StopTimerSecs(timer)/n-overhead;
  853.         ffprintf(o,"%11.3f µs    i=nrand(127);\n",s*1e6);
  854.     
  855.         StartTimer(timer);
  856.         for(iL=n/10;iL>0;iL--){
  857.             i=127L*randU()>>16;
  858.             i=127L*randU()>>16;
  859.             i=127L*randU()>>16;
  860.             i=127L*randU()>>16;
  861.             i=127L*randU()>>16;
  862.             i=127L*randU()>>16;
  863.             i=127L*randU()>>16;
  864.             i=127L*randU()>>16;
  865.             i=127L*randU()>>16;
  866.             i=127L*randU()>>16;
  867.         }
  868.         s=StopTimerSecs(timer)/n-overhead;
  869.         ffprintf(o,"%11.3f µs    i=127L*randU()>>16;\n",s*1e6);
  870.     
  871.         StartTimer(timer);
  872.         for(iL=n/10;iL>0;iL--){
  873.             i=127L*rand()>>15;
  874.             i=127L*rand()>>15;
  875.             i=127L*rand()>>15;
  876.             i=127L*rand()>>15;
  877.             i=127L*rand()>>15;
  878.             i=127L*rand()>>15;
  879.             i=127L*rand()>>15;
  880.             i=127L*rand()>>15;
  881.             i=127L*rand()>>15;
  882.             i=127L*rand()>>15;
  883.         }
  884.         s=StopTimerSecs(timer)/n-overhead;
  885.         ffprintf(o,"%11.3f µs    i=127L*rand()>>15;\n",s*1e6);
  886.     
  887.         StartTimer(timer);
  888.         for(iL=n/10;iL>0;iL--){
  889.             i=randU()%(unsigned short)127;
  890.             i=randU()%(unsigned short)127;
  891.             i=randU()%(unsigned short)127;
  892.             i=randU()%(unsigned short)127;
  893.             i=randU()%(unsigned short)127;
  894.             i=randU()%(unsigned short)127;
  895.             i=randU()%(unsigned short)127;
  896.             i=randU()%(unsigned short)127;
  897.             i=randU()%(unsigned short)127;
  898.             i=randU()%(unsigned short)127;
  899.         }
  900.         s=StopTimerSecs(timer)/n-overhead;
  901.         ffprintf(o,"%11.3f µs    i=randU()%%(unsigned short)127;\n",s*1e6);
  902.     
  903.         StartTimer(timer);
  904.         for(iL=n/10;iL>0;iL--){
  905.             i=rand()%127;
  906.             i=rand()%127;
  907.             i=rand()%127;
  908.             i=rand()%127;
  909.             i=rand()%127;
  910.             i=rand()%127;
  911.             i=rand()%127;
  912.             i=rand()%127;
  913.             i=rand()%127;
  914.             i=rand()%127;
  915.         }
  916.         s=StopTimerSecs(timer)/n-overhead;
  917.         ffprintf(o,"%11.3f µs    i=rand()%%127;\n",s*1e6);
  918.     }
  919.     DisposeTimer(timer);
  920.     fclose(dataFile);    /* close “TimeCPU results” */
  921.     DrawMenuBar();
  922. }
  923.  
  924.